package chapterSystem.client.callbacks;

import chapterSystem.client.model.entitiesInt.Report;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.StackLayoutPanel;

import java.util.List;

/**
 * User: Hasp
 * Date: 06.05.12
 * Time: 5:31
 */
public class ReportsCallback implements AsyncCallback<List<Report>> {
    final private StackLayoutPanel workArea;

    public ReportsCallback(StackLayoutPanel workArea) {
        this.workArea = workArea;
    }


    @Override
    public void onFailure(Throwable caught) {
        workArea.clear();
        workArea.add(new HTML(), new HTML("Failed to connect to server"), 4);
    }

    @Override
    public void onSuccess(List<Report> result) {
        workArea.clear();

        StackLayoutPanel reports = new StackLayoutPanel(Style.Unit.MM);
        if (result != null) {
            for (Report report : result) {
                reports.add(new HTML(report.getReport()), new HTML(report.getDate().toString()),  8);
            }
        }

        if (result == null || result.size() == 0) {      //todo
            workArea.add(new HTML("No reports for you yet, brother."), new HTML("Read Reports"), 4);
        } else {
            workArea.add(reports, new HTML("Read Reports"), 4);
        }
    }
}
